import 'package:flutter/material.dart';
import 'package:zyll_book/app/searchPage.dart';
import 'package:zyll_book/message/conversationList.dart';

import 'FindPage.dart';
import 'SettingsPage.dart';
import 'addressBookPage.dart';



class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
        title: "自由浏览",
        home: new TabsPage(),
        // home: new BottomNavigationBar(items: )
      debugShowCheckedModeBanner: false,
    );// TODO: implement build
    throw UnimplementedError();
  }

}


class TabsPage extends StatefulWidget {
  // TabsPage({required Key key}) : super(key: key);

  @override
  _TabsPageState createState() => _TabsPageState();
}


class _TabsPageState extends State<TabsPage> with RestorationMixin{
  int currentIndex = 0;
  List listTabs = [
    MyStatefulWidget(),
    addressBookPage(),
    FindPage(),
    SettingsPage(),
  ];

  late RestorableRouteFuture<int> _counterRoute;



  @override
  Widget build(BuildContext context) {
    print(currentIndex);
    return Scaffold(
      appBar: PreferredSize(
        preferredSize: Size.fromHeight(45),
        child: Offstage(
          offstage: currentIndex == 3?true:false,
          child: AppBar(
              backgroundColor: Colors.grey.shade300,
              title: Text('消息',style: TextStyle(letterSpacing: 1,fontWeight: FontWeight.w500,fontSize: 20,color: Colors.black),),
              centerTitle: true,
              actions: <Widget>[
                IconButton(
                  color: Colors.black,
                  icon: const Icon(Icons.search),
                  tooltip: '搜索',
                  onPressed: () {
                    print("按下============");
                    // ScaffoldMessenger.of(context).showSnackBar(
                    //     const SnackBar(content: Text('This is a snackbar')));
                    _counterRoute.present('搜索');
                  },
                ),
                PopupMenuButton(
                  color: Colors.black,
                  icon: new Icon(Icons.add_circle_outline,color: Colors.black,),
                  tooltip: '菜单',
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10),
                    side: BorderSide(
                      width: 2,
                      color: Colors.black54,
                      style: BorderStyle.none,
                    ),
                  ),
                  itemBuilder: (BuildContext context) {
                  return [
                    new PopupMenuItem<String>(
                        value: 'value01',
                        child: Row(children: <Widget>[
                          Padding( padding: EdgeInsets.fromLTRB(0.0, 0.0, 8.0, 0.0),
                              child: Icon(Icons.chat_bubble_outline_rounded)),
                          Text('发起群聊',style: TextStyle(color: Colors.white,),)
                        ]),
                      onTap: (){ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Row(children: <Widget>[
                        // Icon(Icons.check,color: Colors.green,),
                        Text('发起群聊')],), duration: Duration(seconds: 1),));},
                    ),


                    new PopupMenuItem<String>(
                        value: 'value01',
                        child: Row(children: <Widget>[
                          Padding( padding: EdgeInsets.fromLTRB(0.0, 0.0, 8.0, 0.0),
                              child: Icon(Icons.chat_bubble_outline_rounded)),
                          Text('添加好友',style: TextStyle(color: Colors.white,),)
                        ]),
                      onTap: (){ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Row(children: <Widget>[
                        // Icon(Icons.check,color: Colors.green,),
                        Text('添加好友')],), duration: Duration(seconds: 1),));},
                    ),


                    new PopupMenuItem<String>(
                        value: 'value01',
                        child: Row(children: <Widget>[
                          Padding( padding: EdgeInsets.fromLTRB(0.0, 0.0, 8.0, 0.0),
                              child: Icon(Icons.chat_bubble_outline_rounded)),
                          Text('扫一扫',style: TextStyle(color: Colors.white,),)
                        ]),
                      onTap: (){ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Row(children: <Widget>[
                        // Icon(Icons.check,color: Colors.green,),
                        Text('扫一扫')],), duration: Duration(seconds: 1),));},
                    ),


                    new PopupMenuItem<String>(
                        value: 'value01',
                        child: Row(children: <Widget>[
                          Padding( padding: EdgeInsets.fromLTRB(0.0, 0.0, 8.0, 0.0),
                              child: Icon(Icons.chat_bubble_outline_rounded)),
                          Text('收付款',style: TextStyle(color: Colors.white,),)
                        ]),
                      onTap: (){ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Row(children: <Widget>[
                      // Icon(Icons.check,color: Colors.green,),
                      Text('收付款')],), duration: Duration(seconds: 1),));},
                    ),

                    new PopupMenuItem<String>(
                        value: 'value01',
                        child: Row(children: <Widget>[
                          Padding( padding: EdgeInsets.fromLTRB(0.0, 0.0, 8.0, 0.0),
                              child: Icon(Icons.chat_bubble_outline_rounded)),
                          Text('帮助与反馈',style: TextStyle(color: Colors.white,),)
                        ]),
                      onTap: (){ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Row(children: <Widget>[
                      // Icon(Icons.check,color: Colors.green,),
                      Text('帮助与反馈')],), duration: Duration(seconds: 1),));},
                    ),


                  ];
                },

                ),
              ],
          ),
        )
      ),

      // AppBar(
      //   title: Text('消息'),
      //   centerTitle: true,
      //   automaticallyImplyLeading: false,

      // ),
      body: this.listTabs[this.currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: this.currentIndex,
        iconSize: 30.0,
        type: BottomNavigationBarType.fixed,
        onTap: (index) {
          setState(() {
            this.currentIndex = index;
          });
        },

        items: [
          BottomNavigationBarItem(
            icon: Icon(Icons.textsms),
            label: "消息",
            // title: Text('消息'),
            tooltip: "消息"
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.people),
              label: "通讯录",
              tooltip: "通讯录"
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.ballot),
            label: "发现",
            // title: Text('发现'),
              tooltip: "发现"
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.person),
            label: "个人信息",
            // title: Text('个人信息'),
              tooltip: "个人信息"
          ),
        ],
      ),
    );
  }

  final RestorableInt _lastCount = RestorableInt(0);
  @override
  String get restorationId => 'home';

  @override
  void initState() {
    super.initState();
    _counterRoute = RestorableRouteFuture<int>(
        onPresent: (NavigatorState navigator, Object? arguments) {
          // Defines what route should be shown (and how it should be added
          // to the navigator) when `RestorableRouteFuture.present` is called.
          return navigator.restorablePush(
            _counterRouteBuilder,
            arguments: arguments,
          );
        }, onComplete: (int count) {
      // Defines what should happen with the return value when the route
      // completes.
      setState(() {
        _lastCount.value = count;
      });
    });
  }

  @override
  void restoreState(RestorationBucket? oldBucket, bool initialRestore) {
    // Register the `RestorableRouteFuture` with the state restoration framework.
    registerForRestoration(_counterRoute, 'route');
    registerForRestoration(_lastCount, 'count');
  }

  @override
  void dispose() {
    super.dispose();
    _lastCount.dispose();
    _counterRoute.dispose();
  }

  // A static `RestorableRouteBuilder` that can re-create the route during
  // state restoration.
  static Route<int> _counterRouteBuilder(
      BuildContext context, Object? arguments) {
    return MaterialPageRoute<int>(
      builder: (BuildContext context) => MyCounter(
        title: arguments!.toString(),
      ),
    );
  }

}
